<template>
  <el-radio-group
    v-model="modelValue"
    @change="handleChange"
  >
    <el-radio
      v-for="item in options" 
      :key="item.key"
      :value="item.key"
    >{{item.value}}</el-radio>
  </el-radio-group>
</template>

<script setup>
import { ref, watch } from 'vue';

const modelValue = defineModel();
const emit = defineEmits(['change']);
const props = defineProps({
  options: [Array, String],
});

const options = ref([]);

const handleChange = () => {
  emit('change');
};

const init = (data) => {
  if (data) {
    if (Array.isArray(data)) {
      options.value = data;
    } else if (typeof data === 'string') {
      const optionList = [];
      data.split(',').forEach(item => {
        optionList.push({key: item, value: item});
      });
      options.value = optionList;
    }
  }
};

watch(() => props.options, (data) => {
  init(data);
}, {immediate: true});
</script>